//
//  RestaurantExtension.m
//  VietQuan
//
//  Created by hainguyen on 11/1/11.
//  Copyright (c) 2011 __MyCompanyName__. All rights reserved.
//

#import "RestaurantExtension.h"
#import "AppDelegate_Shared.h"
#import "CoreDataHelper.h"

@implementation Restaurant(Extension)

#pragma mark Init Method

+ (BOOL) initRestaurent{
	NSError *err = nil;
	NSManagedObjectContext *managedObjectContext = [mainSharedAppDelegate managedObjectContext];
	if (managedObjectContext == nil) {
		return NO;
	}
	NSMutableArray *restArray = [[[NSMutableArray alloc] init]autorelease];
	NSMutableDictionary *restDict = [[NSMutableDictionary alloc] init];
	[restDict setObject:[NSNumber numberWithInt:1] forKey:@"id"];
	[restDict setObject:@"Ốc Xinh" forKey:@"name"];
	[restDict setObject:@"286 Cao Đạt" forKey:@"streetName"];
	[restDict setObject:[NSNumber numberWithInt:4] forKey:@"districtId"];
	[restDict setObject:@"Ốc, Hải sản" forKey:@"tags"];
	[restArray addObject:restDict];
	[restDict release];
	
	restDict = [[NSMutableDictionary alloc] init];
	[restDict setObject:[NSNumber numberWithInt:2] forKey:@"id"];
	[restDict setObject:@"Hoàng Ty " forKey:@"name"];
	[restDict setObject:@"691B/9 Bình Quới" forKey:@"streetName"];
	[restDict setObject:[NSNumber numberWithInt:11] forKey:@"districtId"];
	[restDict setObject:@"Chè, Giải khát" forKey:@"tags"];
	[restArray addObject:restDict];
	[restDict release];
	
	restDict = [[NSMutableDictionary alloc] init];
	[restDict setObject:[NSNumber numberWithInt:3] forKey:@"id"];
	[restDict setObject:@"Quán Mì Tôm Singapore" forKey:@"name"];
	[restDict setObject:@"377b, Nguyễn Tri Phương " forKey:@"streetName"];
	[restDict setObject:[NSNumber numberWithInt:9] forKey:@"districtId"];
	[restDict setObject:@"Ăn nhẹ, Mì, Bún" forKey:@"tags"];
	[restArray addObject:restDict];
	[restDict release];
	
	restDict = [[NSMutableDictionary alloc] init];
	[restDict setObject:[NSNumber numberWithInt:4] forKey:@"id"];
	[restDict setObject:@"Nhà hàng Liên Hoa Chay" forKey:@"name"];
	[restDict setObject:@"497/35 Sư Vạn Hạnh " forKey:@"streetName"];
	[restDict setObject:[NSNumber numberWithInt:9] forKey:@"districtId"];
	[restDict setObject:@"Chay, Gà" forKey:@"tags"];
	[restArray addObject:restDict];
	[restDict release];
	
	restDict = [[NSMutableDictionary alloc] init];
	[restDict setObject:[NSNumber numberWithInt:5] forKey:@"id"];
	[restDict setObject:@"Chè Thập Cẩm Hà Nội" forKey:@"name"];
	[restDict setObject:@"458B Nguyễn Tri Phương" forKey:@"streetName"];
	[restDict setObject:[NSNumber numberWithInt:9] forKey:@"districtId"];
	[restDict setObject:@"Chè, Giải Khát" forKey:@"tags"];
	[restArray addObject:restDict];
	[restDict release];
	
	restDict = [[NSMutableDictionary alloc] init];
	[restDict setObject:[NSNumber numberWithInt:6] forKey:@"id"];
	[restDict setObject:@"Bánh Canh Trảng Bàng" forKey:@"name"];
	[restDict setObject:@"411 - 413 Nguyễn Tri Phương" forKey:@"streetName"];
	[restDict setObject:[NSNumber numberWithInt:9] forKey:@"districtId"];
	[restDict setObject:@"Ăn nhẹ, Đặc sản, bún, bánh canh" forKey:@"tags"];
	[restArray addObject:restDict];
	[restDict release];
	
	restDict = [[NSMutableDictionary alloc] init];
	[restDict setObject:[NSNumber numberWithInt:7] forKey:@"id"];
	[restDict setObject:@"Nhà hàng Biển Đông" forKey:@"name"];
	[restDict setObject:@"410 – 412 An Dương Vương" forKey:@"streetName"];
	[restDict setObject:[NSNumber numberWithInt:4] forKey:@"districtId"];
	[restDict setObject:@"Ăn nhẹ, cháo, cao cấp" forKey:@"tags"];
	[restArray addObject:restDict];
	[restDict release];
	
	restDict = [[NSMutableDictionary alloc] init];
	[restDict setObject:[NSNumber numberWithInt:8] forKey:@"id"];
	[restDict setObject:@"Quán Bột" forKey:@"name"];
	[restDict setObject:@"83E Nguyễn Văn Đậu" forKey:@"streetName"];
	[restDict setObject:[NSNumber numberWithInt:11] forKey:@"districtId"];
	[restDict setObject:@"đặc sản" forKey:@"tags"];
	[restArray addObject:restDict];
	[restDict release];
	
	
	restDict = [[NSMutableDictionary alloc] init];
	[restDict setObject:[NSNumber numberWithInt:9] forKey:@"id"];
	[restDict setObject:@"Quán bún nước lèo Sóc Trăng" forKey:@"name"];
	[restDict setObject:@"78 đường số 1, Khu Trung Sơn" forKey:@"streetName"];
	[restDict setObject:[NSNumber numberWithInt:17] forKey:@"districtId"];
	[restDict setObject:@"đặc sản, bún" forKey:@"tags"];
	[restArray addObject:restDict];
	[restDict release];
	
	restDict = [[NSMutableDictionary alloc] init];
	[restDict setObject:[NSNumber numberWithInt:10] forKey:@"id"];
	[restDict setObject:@"Quán Ở Quãy" forKey:@"name"];
	[restDict setObject:@"535 3 tháng 2" forKey:@"streetName"];
	[restDict setObject:[NSNumber numberWithInt:9] forKey:@"districtId"];
	[restDict setObject:@"đặc sản, bún" forKey:@"tags"];
	[restArray addObject:restDict];
	[restDict release];
	
	restDict = [[NSMutableDictionary alloc] init];
	[restDict setObject:[NSNumber numberWithInt:11] forKey:@"id"];
	[restDict setObject:@"Sushi & Que" forKey:@"name"];
	[restDict setObject:@"190 Thành Thái" forKey:@"streetName"];
	[restDict setObject:[NSNumber numberWithInt:9] forKey:@"districtId"];
	[restDict setObject:@"Sushi, Đồ ăn Ngoại" forKey:@"tags"];
	[restArray addObject:restDict];
	[restDict release];
	
	restDict = [[NSMutableDictionary alloc] init];
	[restDict setObject:[NSNumber numberWithInt:12] forKey:@"id"];
	[restDict setObject:@"Nem nướng Ninh Hoà" forKey:@"name"];
	[restDict setObject:@"940 3 tháng 2" forKey:@"streetName"];
	[restDict setObject:[NSNumber numberWithInt:10] forKey:@"districtId"];
	[restDict setObject:@"nem nướng, đặt sản" forKey:@"tags"];
	[restArray addObject:restDict];
	[restDict release];
	
	restDict = [[NSMutableDictionary alloc] init];
	[restDict setObject:[NSNumber numberWithInt:13] forKey:@"id"];
	[restDict setObject:@"Quán ốc Xuân" forKey:@"name"];
	[restDict setObject:@"B30, Thành Thái" forKey:@"streetName"];
	[restDict setObject:[NSNumber numberWithInt:9] forKey:@"districtId"];
	[restDict setObject:@"Ốc, hải sản" forKey:@"tags"];
	[restArray addObject:restDict];
	[restDict release];
	
	restDict = [[NSMutableDictionary alloc] init];
	[restDict setObject:[NSNumber numberWithInt:14] forKey:@"id"];
	[restDict setObject:@"Cà ri Dê Musa" forKey:@"name"];
	[restDict setObject:@"001 lô B chung cư Sư Vạn Hạnh" forKey:@"streetName"];
	[restDict setObject:[NSNumber numberWithInt:4] forKey:@"districtId"];
	[restDict setObject:@"bình dân, vỉa hè, nướng" forKey:@"tags"];
	[restArray addObject:restDict];
	[restDict release];
	
	restDict = [[NSMutableDictionary alloc] init];
	[restDict setObject:[NSNumber numberWithInt:15] forKey:@"id"];
	[restDict setObject:@"Quán Hải Tứ Quý" forKey:@"name"];
	[restDict setObject:@"263 Võ Văn Tần" forKey:@"streetName"];
	[restDict setObject:[NSNumber numberWithInt:2] forKey:@"districtId"];
	[restDict setObject:@"Bún, phở, Bún cá" forKey:@"tags"];
	[restArray addObject:restDict];
	[restDict release];
	
	restDict = [[NSMutableDictionary alloc] init];
	[restDict setObject:[NSNumber numberWithInt:16] forKey:@"id"];
	[restDict setObject:@"Quán Ốc" forKey:@"name"];
	[restDict setObject:@"273 Hoàng Diệu " forKey:@"streetName"];
	[restDict setObject:[NSNumber numberWithInt:3] forKey:@"districtId"];
	[restDict setObject:@"Ốc, hải sản" forKey:@"tags"];
	[restArray addObject:restDict];
	[restDict release];
	
	restDict = [[NSMutableDictionary alloc] init];
	[restDict setObject:[NSNumber numberWithInt:17] forKey:@"id"];
	[restDict setObject:@"Quán nem nướng Quen" forKey:@"name"];
	[restDict setObject:@"150/47 Nguyễn Trãi" forKey:@"streetName"];
	[restDict setObject:[NSNumber numberWithInt:0] forKey:@"districtId"];
	[restDict setObject:@"nướng. nhẹ, bình dân" forKey:@"tags"];
	[restArray addObject:restDict];
	[restDict release];
	
	restDict = [[NSMutableDictionary alloc] init];
	[restDict setObject:[NSNumber numberWithInt:18] forKey:@"id"];
	[restDict setObject:@"Quán ốc số 9" forKey:@"name"];
	[restDict setObject:@"23 Nguyễn Văn Giai" forKey:@"streetName"];
	[restDict setObject:[NSNumber numberWithInt:0] forKey:@"districtId"];
	[restDict setObject:@"ốc. nhẹ, bình dân" forKey:@"tags"];
	[restArray addObject:restDict];
	[restDict release];
	
	restDict = [[NSMutableDictionary alloc] init];
	[restDict setObject:[NSNumber numberWithInt:19] forKey:@"id"];
	[restDict setObject:@"Quán Hủ tíu mì thập cẩm" forKey:@"name"];
	[restDict setObject:@"62 Trương Định," forKey:@"streetName"];
	[restDict setObject:[NSNumber numberWithInt:0] forKey:@"districtId"];
	[restDict setObject:@"Hủ Tiếu, mì, bún nhẹ, bình dân" forKey:@"tags"];
	[restArray addObject:restDict];
	[restDict release];
	
	restDict = [[NSMutableDictionary alloc] init];
	[restDict setObject:[NSNumber numberWithInt:20] forKey:@"id"];
	[restDict setObject:@"Bún chả cá Đà Nẵng" forKey:@"name"];
	[restDict setObject:@"321 Điện Biên Phủ" forKey:@"streetName"];
	[restDict setObject:[NSNumber numberWithInt:2] forKey:@"districtId"];
	[restDict setObject:@"Hủ Tiếu, mì, bún nhẹ" forKey:@"tags"];
	[restArray addObject:restDict];
	[restDict release];
	
	restDict = [[NSMutableDictionary alloc] init];
	[restDict setObject:[NSNumber numberWithInt:21] forKey:@"id"];
	[restDict setObject:@"Cà phê nghệ thuật Um" forKey:@"name"];
	[restDict setObject:@"16/2 Trương Định" forKey:@"streetName"];
	[restDict setObject:[NSNumber numberWithInt:2] forKey:@"districtId"];
	[restDict setObject:@"café" forKey:@"tags"];
	[restArray addObject:restDict];
	[restDict release];
	
	restDict = [[NSMutableDictionary alloc] init];
	[restDict setObject:[NSNumber numberWithInt:22] forKey:@"id"];
	[restDict setObject:@"Vịt 3 món" forKey:@"name"];
	[restDict setObject:@"25/18 Lương Định Của" forKey:@"streetName"];
	[restDict setObject:[NSNumber numberWithInt:1] forKey:@"districtId"];
	[restDict setObject:@"vịt, đặc sản" forKey:@"tags"];
	[restArray addObject:restDict];
	[restDict release];
	
	restDict = [[NSMutableDictionary alloc] init];
	[restDict setObject:[NSNumber numberWithInt:23] forKey:@"id"];
	[restDict setObject:@"Quán mì vịt tiềm" forKey:@"name"];
	[restDict setObject:@"93 Thành Thái" forKey:@"streetName"];
	[restDict setObject:[NSNumber numberWithInt:9] forKey:@"districtId"];
	[restDict setObject:@"vịt, đặc sản" forKey:@"tags"];
	[restArray addObject:restDict];
	[restDict release];
	
	restDict = [[NSMutableDictionary alloc] init];
	[restDict setObject:[NSNumber numberWithInt:24] forKey:@"id"];
	[restDict setObject:@"Mì Quảng Ngon Phố Thị" forKey:@"name"];
	[restDict setObject:@"110 Cách Mạng Tháng 8" forKey:@"streetName"];
	[restDict setObject:[NSNumber numberWithInt:2] forKey:@"districtId"];
	[restDict setObject:@"mì,bánh, bún, phở đặc sản" forKey:@"tags"];
	[restArray addObject:restDict];
	[restDict release];
	
	for (int i = 0; i < [restArray count]; i++) {
		NSMutableDictionary *dict = [restArray objectAtIndex:i];
		Restaurant *restaurant = [NSEntityDescription insertNewObjectForEntityForName:@"Restaurant" inManagedObjectContext:managedObjectContext];
		restaurant.id = [dict objectForKey:@"id"];
		restaurant.name = [dict objectForKey:@"name"];
		restaurant.streetName = [dict objectForKey:@"streetName"];
		restaurant.districtId = [dict objectForKey:@"districtId"];
		restaurant.tags = [dict objectForKey:@"tags"];
	}
	[managedObjectContext save:&err];
	return YES;
}

#pragma mark Get Methods 

+ (NSArray *) getRestaurants{
	NSManagedObjectContext *managedObjectContext = [mainSharedAppDelegate managedObjectContext];
	if (managedObjectContext == nil){
		return nil;	
	}
	NSArray *lists;
	@try {
		lists = (NSMutableArray*) [CoreDataHelper searchObjectsInContext:@"Restaurant" :nil :nil :NO :managedObjectContext];
	}@catch (NSException * e) {
		NSLog(@"%@",[e description]);
    }
	
	return lists;
}

+ (Restaurant *) getRestaurantWithId:(int)restaurantId{
	Restaurant* restaurant = nil;
	NSManagedObjectContext *managedObjectContext = [mainSharedAppDelegate managedObjectContext];
	if (managedObjectContext == nil) {
		return nil;
	}	
	NSMutableArray *restaurantArray;
	NSPredicate *predicate = [NSPredicate predicateWithFormat: @"id = %d", restaurantId];	
	@try {
		restaurantArray = (NSMutableArray*) [CoreDataHelper searchObjectsInContext:@"Restaurant" :predicate :nil :NO :managedObjectContext];
		
		if ([restaurantArray count] != 0)
		{
			restaurant = [restaurantArray objectAtIndex:0];
		}
	} @catch (NSException * e) {
		NSLog(@"error: %@", [e description]);
	}
	
	return restaurant;
}

#pragma mark search Method

+ (NSArray*)searchByName:(NSString*) keyword{
	NSManagedObjectContext *managedObjectContext = [mainSharedAppDelegate managedObjectContext];
    if (managedObjectContext == nil)
    {
        return nil;	
    }
    NSArray *resultArray;	
    NSPredicate *predicate = [NSPredicate predicateWithFormat: @"name contains[cd] %@", keyword];
    @try 
    {
        resultArray = (NSMutableArray*) [CoreDataHelper searchObjectsInContext:@"Restaurant" :predicate :nil :NO :managedObjectContext];
    }
    @catch (NSException * e) 
    {
        NSLog(@"search by name error: %@", [e description]);
    }
    
    return resultArray;
}

+ (NSArray*)searchByTag:(NSString*) keyword{
	NSManagedObjectContext *managedObjectContext = [mainSharedAppDelegate managedObjectContext];
    if (managedObjectContext == nil)
    {
        return nil;	
    }
    NSArray *resultArray;	
    NSPredicate *predicate = [NSPredicate predicateWithFormat: @"tags contains[cd] %@", keyword];
    @try 
    {
        resultArray = (NSMutableArray*) [CoreDataHelper searchObjectsInContext:@"Restaurant" :predicate :nil :NO :managedObjectContext];
    }
    @catch (NSException * e) 
    {
        NSLog(@"search by tags error: %@", [e description]);
    }
    
    return resultArray;
}

+ (NSArray*)searchByStreetName:(NSString*) keyword{
	NSManagedObjectContext *managedObjectContext = [mainSharedAppDelegate managedObjectContext];
    if (managedObjectContext == nil)
    {
        return nil;	
    }
    NSArray *resultArray;	
    NSPredicate *predicate = [NSPredicate predicateWithFormat: @"streetName contains[cd] %@", keyword];
    @try 
    {
        resultArray = (NSMutableArray*) [CoreDataHelper searchObjectsInContext:@"Restaurant" :predicate :nil :NO :managedObjectContext];
    }
    @catch (NSException * e) 
    {
        NSLog(@"search by streetName error: %@", [e description]);
    }
    
    return resultArray;
}


@end
